<?php
class Usersmodel extends CI_Model{
	function insert_main($data){
		//begin trans

		$this->db->trans_start();

		$insert_uuid = $this->insert_vendor($data);
		$this->insert_vendor_address($data, $insert_uuid);
		$this->insert_vendor_addressreg($data, $insert_uuid);
		$this->insert_vendor_upload($insert_uuid);


		$this->db->trans_complete();

		return $insert_uuid;

		//end trans

	}

	function insert_vendor($data){

		$insertdata = array(
			'vfirstname' => $data['vfirstname'] ,
			'vlastname' => $data['vlastname'] ,
			'vgender' => $data['vgender'] ,
			'vbirthdate' => $data['vbirthdate'],
			'vtype' => $data['vtype'],
			'vemail' => $data['vemail'],
			'vphone' => $data['vphone'],
			'vmobile' => $data['vmobile'],
			'vfax' => $data['vfax'],
			'vidcard' => $data['idcard'],
		);

		$objreturn = $this->db->insert('vendor',$insertdata);
		return $this->db->insert_id();
	}

	function insert_vendor_address($data,$insert_uuid){
		$insertdata = array(
			'uuid' => $insert_uuid,
			'housenum' => $data['housenum'],
			'village' => $data['village'],
			'moono' => $data['moono'],
			'soi' => $data['soi'],
			'street' => $data['street'],
			'district' => $data['district'],
			'city' => $data['city'],
			'province' => $data['province'],
			'zipcode' => $data['zipcode']
		);
		$this->db->insert('vendor_address',$insertdata);
	}
	
	function insert_vendor_addressreg($data,$insert_uuid){
		if ($this->input->post('issame') == 'issame'){
		
		$insertdata = array(
			'uuid' => $insert_uuid,
			'housenum' => $data['housenum'],
			'village' => $data['village'],
			'moono' => $data['moono'],
			'soi' => $data['soi'],
			'street' => $data['street'],
			'district' => $data['district'],
			'city' => $data['city'],
			'province' => $data['province'],
			'zipcode' => $data['zipcode']
		);
		}
		else{
			$insertdata = array(
			'uuid' => $insert_uuid,
			'housenum' => $data['housenum2'],
			'village' => $data['village2'],
			'moono' => $data['moono2'],
			'soi' => $data['soi2'],
			'street' => $data['street2'],
			'district' => $data['district2'],
			'city' => $data['city2'],
			'province' => $data['province2'],
			'zipcode' => $data['zipcode2']
		);
		}
		$this->db->insert('vendor_address',$insertdata);
	}

	function insert_vendor_code($data,$insert_uuid){
		$this->db->set('uuid', $insert_uuid);
		$this->db->set('vtype', $data['vtype']);
		$this->db->set('vyear', 'substring(year(now())+543,3)',FALSE);
		$this->db->insert('vendor_code');

		$this->db->set('vcode','concat(vtype,vyear,LPAD(vid,5,0))',FALSE);
		$this->db->where('uuid',$insert_uuid);
		$this->db->update('vendor_code');

		$this->db->select('vcode');
		$query = $this->db->get_where('vendor_code',array('uuid'=>$insert_uuid));
		$row = $query->row_array();
		$gen_vcode = $row['vcode'];

		$this->db->set('vendorcode',$gen_vcode);
		$this->db->where('uuid',$insert_uuid);
		$this->db->update('vendor');
	}

	function insert_vendor_upload($insert_uuid){
		$this->load->library('upload');

		$config['upload_path'] = './uploads/'.$insert_uuid.'/';
		mkdir($config['upload_path'], 0777);

		$config['allowed_types'] = 'gif|jpg|png';
		$config['file_name'] = 'file1';
		$config['overwrite'] = TRUE;

		$this->upload->initialize($config);
		if ($this->upload->do_upload('file1')){
			$success_data = $this->upload->data();
			$upload_path['file1'] = base_url("uploads/$insert_uuid/{$success_data['orig_name']}");
		}else{
			$upload_path['file1'] = '';
		}

		$config['file_name'] = 'file2';
		$this->upload->initialize($config);
		if ($this->upload->do_upload('file2')) {
			$success_data = $this->upload->data();
			$upload_path['file2'] = base_url("uploads/$insert_uuid/{$success_data['orig_name']}");
		}else{
			$upload_path['file2'] = '';
		}



		$config['file_name'] = 'file3';
		$this->upload->initialize($config);
		if ($this->upload->do_upload('file3')) {
			$success_data = $this->upload->data();
			$upload_path['file3'] = base_url("uploads/$insert_uuid/{$success_data['orig_name']}");
		}else{
			$upload_path['file3'] = '';
		}
		//print_r($upload_path);
		$this->db->set('uuid', $insert_uuid);
		$this->db->set('file1', $upload_path['file1']);
		$this->db->set('file2', $upload_path['file2']);
		$this->db->set('file3', $upload_path['file3']);
		$this->db->insert('vendor_upload');
	}

	function get_user($uuid){
		$this->db->select('*');
		$this->db->from('vendor');
		$this->db->join('vendor_address', 'vendor.uuid = vendor_address.uuid');
		$this->db->join('vendor_upload', 'vendor.uuid = vendor_upload.uuid');
		$this->db->join('option_village','vendor_address.village = option_village.villageid');
		$this->db->join('type_option','vendor.vtype = type_option.typeid');
		$this->db->where('vendor.uuid',$uuid);

		$query = $this->db->get();
		$row = $query->row_array();
		//print_r($row);
		return $row;
	}
	
	function get_user_truck_summary($vendorcode){
		$this->db->select('
							sum(buynet1) as sum_netbuy,
							sum(quantity_prom) as sum_promo,
							sum(net_wght1) as sum_netwg,
						',FALSE);
		$this->db->from('truck');
		$this->db->where('vendorid',$vendorcode);
		return $this->db->get()->row_array();
	}

	function update_main($data){
		//begin trans

		$this->db->trans_start();

		$this->update_vendor($data);
		$this->update_vendor_address($data);
		$this->update_vendor_addressreg($data);
		$this->update_vendor_upload($data['uuid']);

		$this->db->trans_complete();

		//end trans

	}

	function update_vendor($data){

		$updatedata = array(
			'vfirstname' => $data['vfirstname'] ,
			'vlastname' => $data['vlastname'] ,
			'vgender' => $data['vgender'] ,
			'vbirthdate' => $data['vbirthdate'],
			'vtype' => $data['vtype'],
			'vemail' => $data['vemail'],
			'vphone' => $data['vphone'],
			'vmobile' => $data['vmobile'],
			'vfax' => $data['vfax'],
			'vidcard' => $data['idcard'],
		);

		$this->db->where('uuid', $data['uuid']);
		$this->db->update('vendor', $updatedata);
	}

	function update_vendor_address($data){
		$updatedata = array(
			'housenum' => $data['housenum'],
			'village' => $data['village'],
			'moono' => $data['moono'],
			'soi' => $data['soi'],
			'street' => $data['street'],
			'district' => $data['district'],
			'city' => $data['city'],
			'province' => $data['province'],
			'zipcode' => $data['zipcode']
		);
		$this->db->where('uuid', $data['uuid']);
		$this->db->update('vendor_address',$updatedata);
	}
	
	function update_vendor_addressreg($data){
		if ($this->input->post('issame') == 'issame'){
		$updatedata = array(
			'housenum' => $data['housenum'],
			'village' => $data['village'],
			'moono' => $data['moono'],
			'soi' => $data['soi'],
			'street' => $data['street'],
			'district' => $data['district'],
			'city' => $data['city'],
			'province' => $data['province'],
			'zipcode' => $data['zipcode']
		);
		}
		else{
			$updatedata = array(
			'housenum' => $data['housenum2'],
			'village' => $data['village2'],
			'moono' => $data['moono2'],
			'soi' => $data['soi2'],
			'street' => $data['street2'],
			'district' => $data['district2'],
			'city' => $data['city2'],
			'province' => $data['province2'],
			'zipcode' => $data['zipcode2']
		);
		}
		$this->db->where('uuid', $data['uuid']);
		$this->db->update('vendor_address',$updatedata);
	}


	function update_vendor_upload($uuid){
		$this->load->library('upload');

		$havetoupdate = FALSE;
		$config['upload_path'] = './uploads/'.$uuid.'/';
		//mkdir($config['upload_path'], 0777);

		$config['allowed_types'] = 'gif|jpg|png';
		$config['file_name'] = 'file1';
		$config['overwrite'] = TRUE;

		$this->upload->initialize($config);
		if ($this->upload->do_upload('file1')){
			$success_data = $this->upload->data();
			$upload_path['file1'] = base_url("uploads/$uuid/{$success_data['orig_name']}");
			$this->db->set('file1', $upload_path['file1']);
			$havetoupdate = TRUE;
		}else{
			//$upload_path['file1'] = '';
		}

		$config['file_name'] = 'file2';
		$this->upload->initialize($config);
		if ($this->upload->do_upload('file2')) {
			$success_data = $this->upload->data();
			$upload_path['file2'] = base_url("uploads/$uuid/{$success_data['orig_name']}");
			$this->db->set('file2', $upload_path['file2']);
			$havetoupdate = TRUE;
		}else{
			//$upload_path['file2'] = '';
		}



		$config['file_name'] = 'file3';
		$this->upload->initialize($config);
		if ($this->upload->do_upload('file3')) {
			$success_data = $this->upload->data();
			$upload_path['file3'] = base_url("uploads/$uuid/{$success_data['orig_name']}");
			$this->db->set('file3', $upload_path['file3']);
			$havetoupdate = TRUE;
		}else{
			//$upload_path['file3'] = '';
		}
		//print_r($upload_path);

		if ($havetoupdate){
			$this->db->where('uuid',$uuid);

			$this->db->update('vendor_upload');
		}
	}
	
	
	function get_group($search){
		$this->db->select("option_village.villageid as villageid,
							villagename,
							vendorcode as headcode,
							concat(vfirstname,' ',vlastname) as headname,
							sum(sum_netwg) as sum_netwg,
							sum(sum_netbuy) as sum_netbuy,
							sum(sum_grouppromo) as sum_grouppromo
							",FALSE);
		$this->db->from('option_village');
		$this->db->join('vendor_address','option_village.villageid = vendor_address.village');
		$this->db->join('vendor','vendor_address.uuid = vendor.uuid');
		$this->db->join('truck_group_pay','option_village.villageid = truck_group_pay.villageid');
		
		if ($search){
			$this->db->like('villagename',$search);
			$this->db->or_like('vfirstname',$search);
			$this->db->or_like('vlastname',$search);
			$this->db->or_like('vendorcode',$search);
		}
		$this->db->where('vendor.vtype','H'); // head of group
		$this->db->order_by('villageid asc');
		return $this->db->get()->result_array();
		//echo $this->db->last_query();
	}
	
function get_group_summary($search){
		$this->db->select("option_village.villageid as villageid,
							villagename,
							vendorcode as headcode,
							concat(vfirstname,' ',vlastname) as headname,
							sum_netwg,
							sum_netbuy,
							sum_grouppromo,
							sum_paid,
							balance
							",FALSE);
		$this->db->from('truck_group_summary');
		$this->db->join('vendor_address','truck_group_summary.villageid = vendor_address.village');
		$this->db->join('vendor','vendor_address.uuid = vendor.uuid');
		$this->db->join('option_village','truck_group_summary.villageid = option_village.villageid');
		if ($search){
			$this->db->like('villagename',$search);
			$this->db->or_like('vfirstname',$search);
			$this->db->or_like('vlastname',$search);
			$this->db->or_like('vendorcode',$search);
		}
		$this->db->where('vendor.vtype','H'); // head of group
		$this->db->order_by('villageid asc');
		return $this->db->get()->result_array();
		//echo $this->db->last_query();
	}
	
	function save_group_payment($village,$money,$whopay,$whoreceive){
		$data = array(
		   'villageid' => $village ,
		   'money' => $money ,
		   'whopay' => $whopay,
			'whoreceive' => $whoreceive
		);
		$this->db->insert('truck_group_paid_log', $data);
		
		
		$sql = "UPDATE truck_larn_pay SET paidamount=paidamount+$money WHERE larn=$pos";
		//update larn paid
//		$this->db->select('paidamount');
//		$this->db->from('truck_larn_pay');
//		$this->db->where('larn',$pos);
//		$result = $this->db->get()->row_array();
//		$havepaid = $result['paidamount'];
//		
//		$this->db->set('paidamount',$havepaid+$money);
//		$this->db->where('larn',$pos);
//		$this->db->update('truck_larn_pay');
		$this->db->simple_query($sql);
		return true;
	}
	
	function get_groupdetail($villageid){
		$this->db->select("
							truck_group_summary.villageid as villageid,
							villagename,
							sum_netwg,
							sum_netbuy,
							sum_grouppromo,
							sum_paid,
							balance,
							vendor.uuid as headuuid,
							vendor.vendorcode as headcode,
							concat(vfirstname,' ',vlastname) as headname,
							housenum as head_housenum,
							village as head_village,
							moono as head_moono,
							street as head_street,
							district as head_district,
							city as head_city,
							province as head_province,
							zipcode as head_zipcode,
							vmobile,
							vphone,
							
		
							",FALSE);
		$this->db->from('truck_group_summary');
		$this->db->join('vendor_address','truck_group_summary.villageid = vendor_address.village');
		$this->db->join('vendor','vendor_address.uuid = vendor.uuid');
		$this->db->join('option_village','truck_group_summary.villageid = option_village.villageid');
		$this->db->where('truck_group_summary.villageid',$villageid);
		$this->db->where('vendor.vtype','H'); // head of group
		$this->db->order_by('villageid asc');
		$result= $this->db->get()->row_array();
		//echo $this->db->last_query();
		return $result;
	}
	
	function get_groupdetail_member($villageid){
		$this->db->select("
							vendor.uuid as uuid,
							vendor.vendorcode as vendorcode,
							concat(vfirstname,' ',vlastname) as vendorname,
							vregisdate as vendor_regisdate,
							sum(net_wght1) as sum_netwg,
		
		",FALSE);
		$this->db->from('vendor_address');
		$this->db->join('vendor','vendor_address.uuid = vendor.uuid');
		$this->db->join('truck','vendor.vendorcode = truck.vendorid','left outer');
		$this->db->where('vendor_address.village',$villageid);
		//$this->db->where('vendor.vtype !=','H');
		$this->db->group_by('vendor.vendorcode');
		$this->db->order_by('vendor.vendorcode asc');
		return $this->db->get()->result_array();
	}
	
	function get_groupdetail_transaction($villageid){
		$this->db->from('truck_group_transaction');
		$this->db->where('villageid',$villageid);
		$this->db->order_by('trans_date desc');
		return $this->db->get()->result_array();
	}
	
	function get_group_paymentdetail($paid_id){
		$query = $this->db->get_where('truck_group_paid_log', array('paid_id' => $paid_id));
		return $query->row_array();
	}
}